package com.faryangsh.controller;

import com.faryangsh.Service.UserService;
import com.faryangsh.bean.User;
import com.faryangsh.bean.one.UserOne;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;

/**
 * @author 杨志远[of2551]
 *         company qianmi.com
 *         Date    2017-07-11
 */
@RestController
@RequestMapping(value = "/users")
@EnableSwagger2
public class UserController
{
    @Autowired
    private UserService userService;

    @ApiOperation("添加user")
    @RequestMapping(value = "/add/{id}/{name}/{address}",method = RequestMethod.POST)
    public UserOne addUser(@PathVariable int id, @PathVariable String name,
                        @PathVariable String address)
    {
        UserOne user = new UserOne();
        user.setId(id);
        user.setName(name);
        user.setAddress(address);
        userService.saveUser(user);
        return user;
    }

    @ApiOperation("删除书")
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
    public void deleteBook(@PathVariable int id)
    {
        userService.delete(id);
    }

    @ApiOperation("查询书")
    @RequestMapping(value = "/",method = RequestMethod.GET)
    public List<UserOne> getBooks()
    {
        return userService.findAll();
    }

    @ApiOperation("查询user")
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public UserOne getUser(@PathVariable int id)
    {
        UserOne user = userService.findOne(id);
        return user;
    }

    @ApiOperation("通过姓名查询人(精确查询)")
    @RequestMapping(value = "/search/name/{name}",method = RequestMethod.GET)
    public List<UserOne> getUserByName(@PathVariable String name)
    {
        List<UserOne> users = userService.findByName(name);
        return users;
    }

    @ApiOperation("通过姓名查询人(模糊查询)")
    @RequestMapping(value = "/search/name/like/{name}",method = RequestMethod.GET)
    public List<UserOne> getUserByLikeName(@PathVariable String name)
    {
        List<UserOne> users = userService.findByNameLike(name);
        return users;
    }

}